package com.ly.sy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.sy.entity.Course;
import com.ly.sy.entity.Xktable;
import com.ly.sy.service.CourseService;
import com.ly.sy.service.VideoService;
import com.ly.sy.service.XktableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2020-11-11
 */
@Controller
@RequestMapping(value = "/sy/course",method = RequestMethod.POST)
public class CourseController {
    //    http://localhost:8080/sy/course/goindex
    @Autowired
    private CourseService courseService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private XktableService xktableService;
    int showAll = 1;
    int showOS = 1;
    int showLinux = 1;
    //课程分页查询跳转到index页面
    @GetMapping("/goindex")
    public String pageindex(HttpSession session){

        showAll=1;
        showOS=1;
        showLinux=1;
        //创建分页对象
        Page<Course> coursePage = new Page<>(1,15);
        //分页的所有数据都封装到coursePage对象中
        courseService.page(coursePage,null);
        long total = coursePage.getTotal();
        List<Course> recordes = coursePage.getRecords();//数据集合
        System.out.println(total);

        List<Course> courseList = courseService.courselikeLise(1,"操作系统",null);
        List<Course> courselikeLise = courseService.courselikeLise(1,"Linux", "linux");
        session.setAttribute("courseList",courseList);
        session.setAttribute("courselikeLise",courselikeLise);
//        System.out.println(recordes);
        session.setAttribute("courses",recordes);

        return "zindex";
    }
//    load more course

    @PostMapping("appendpage")
    @ResponseBody
    public List<Course> appendpage(@RequestParam String courseWho,HttpSession session){
        if(courseWho.equals("showAll")){
            showAll++;
            Page<Course> coursePage = new Page<>(showAll,15);
            //分页的所有数据都封装到coursePage对象中
            courseService.page(coursePage,null);
            long total = coursePage.getTotal();
            List<Course> recordes = coursePage.getRecords();
            session.setAttribute("total",total);
            return recordes;
        }else if(courseWho.equals("showOS")){
            showOS++;
            List<Course> searcList = courseService.courselikeLise(showOS,"操作系统", null);
            session.setAttribute("total",searcList.size());
            System.out.println("操作系统："+searcList.size());
            return searcList;
        }else {
            showLinux++;
            List<Course> searcList = courseService.courselikeLise(showLinux,"Linux", "linux");
            session.setAttribute("total",searcList.size());
            System.out.println("Linux："+searcList.size());
            return searcList;
        }
    }
//    根据courseID查询课程显示详细信息
    @GetMapping("details")
    public String details(@RequestParam Integer id, HttpSession session){
        session.removeAttribute("acourse");

        Course course = new Course();
        course.setCourseId(id);
        Course rest = courseService.selectcourseById(course);
        session.setAttribute("acourse",rest);

        return "redirect:/sy/video/findVideoById?courseId="+id;

    }
//    获取用户选课
    @GetMapping("findusercourse")
    public String findusercourse(HttpSession session){
        List<Xktable> courseidList = (List<Xktable>) session.getAttribute("courseidList");
        List courseids = new ArrayList();
        for (int i=0;i<courseidList.size();i++){
            courseids.add(courseidList.get(i).getCourseId());
        }

        System.out.println("存到courseids中的id："+courseids);
        List<Course> courseList = (List<Course>) courseService.listByIds(courseids);
        System.out.println(courseList);
        session.setAttribute("userscourse",courseList);

        return "User/myclass";
    }

//    开始学习去到课件页面
    @GetMapping("courseware")
    public String courseware(@RequestParam Integer courseid, HttpSession session){
        Course course = new Course();
        course.setCourseId(courseid);
        System.out.println("+++++++++++++++++++++++++++++++++学习+++++++++++++++++++++++++++++++++++++++");
        Course rest = courseService.selectcourseById(course);
        session.setAttribute("coursesv",rest);
        return "redirect:/sy/video/findVideoByIdtwo?courseId="+courseid;
    }
//模糊搜索ajax请求
    @GetMapping("search")
    @ResponseBody
    public List<Course> search(@RequestParam String searchstr, HttpSession session){
        List<Course> searcList = courseService.courselikeLise(0,searchstr, null);
        return searcList;
    }
    //---------------------------------------------以下是admin-------------------------------------------------------------
    //课程管理页面
    @GetMapping("admincourse")
    public String admincourse(HttpSession session){
        List<Course> courseList = courseService.list(null);
        session.setAttribute("courseadmin",courseList);
        return "/admin/courseadmin";
    }
    //添加课程页面
    @GetMapping("courseadd")
    public String courseadd(@RequestParam Integer userId,HttpSession session){
        session.setAttribute("addcourseUserId",userId);
        return "/admin/courseadd";
    }
    //课程发布
    @PostMapping("savecourse")
    @ResponseBody
    public String savecourse(@RequestBody Course course, HttpSession session){
        System.out.println(course.getCourseAuid());
        System.out.println(course.getCourseAuthor());
        System.out.println(course.getCourseDetails());
        System.out.println(course.getCourseName());
        System.out.println(course.getCourseImge());
        courseService.save(course);
        QueryWrapper<Course> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_imge",course.getCourseImge());
        Course course1 = courseService.getOne(queryWrapper);
        System.out.println("===========================课程发布状态显示====================================");
        System.out.println(course1.getCourseId());
        System.out.println(course1.getCourseName());
        System.out.println(course1.getCourseDetails());
        session.setAttribute("courseshow",course1);
        return "ok";
    }
    //跳转到课程概览
    @GetMapping("courseview")
    public String courseview(HttpSession session){

        return "/admin/courseview";
    }
    //去编辑课程
    @GetMapping("goEditorCourse")
    @ResponseBody
    public String goEditorCourse(@RequestParam Integer courseid, HttpSession session){
        Course course = courseService.getById(courseid);
        session.setAttribute("EditCourse",course);
        System.out.println("=========================课程编辑===========================");
        return "/sy/course/EditorCourse";
    }
    @GetMapping("EditorCourse")
    public String EditorCourse(){
        return "/admin/EditorCourse";
    }
    //课程信息修改
    @PostMapping("upcourse")
    @ResponseBody
    public String upcourse(@RequestBody Course course, HttpSession session){
        courseService.updateById(course);
        Course courses = courseService.getById(course.getCourseId());
        session.setAttribute("EditCourse",courses);
        return "ok";
    }
//    ajax异步删除课程
    /**
     * 这里的评论是不需要删的，因为他对数据没有什么影响
     * 本身评论算是用户的数据，而不能算是课程的
     *
     * **/
    @PostMapping("deletecourse")
    @ResponseBody
    public String deletecourse(@RequestParam Integer courseId, HttpSession session){
//        删除课程下的作业
        courseService.deleteHomeworkBycourseId(courseId);
//        删除课程下的视频连同点播上的资源一起删除
        courseService.deletevideoBycourseId(courseId);
        courseService.deletexkBycourseId(courseId);
//        删除课程
        courseService.removeById(courseId);
//        更新显示页面数据
        List<Course> courseList = courseService.list(null);
        session.setAttribute("courseadmin",courseList);
        return "ok";
    }

}

